<template>
  <el-dialog
    title="ERP物料主数据"
    :visible.sync="dialogVisible"
    :modal="true"
    :appendToBody="true"
    width="60%"
    v-dialog-drag
    :close-on-click-modal="false"
    top="5vh"
    class="material-dialog-wrap"
    @close="handleClose()"
    @open="handleOpen()"
  >
    <el-form :model="form" ref="ruleForm" label-width="140px" disabled>
      <el-form-item label="物料编码" prop="erpItemCode">
      <el-input type="text" v-model="form.erpItemCode" placeholder="请输入物料编码"></el-input>
      </el-form-item>
      <el-form-item label="物料名称（中文）" prop="erpItemDescCn">
      <el-input type="text" v-model="form.erpItemDescCn" placeholder="请输入物料名称（中文）"></el-input>
      </el-form-item>
      <el-form-item label="物料名称（英文）" prop="erpItemDescEn">
      <el-input type="text" v-model="form.erpItemDescEn" placeholder="请输入物料名称（英文）"></el-input>
      </el-form-item>
      <el-form-item label="单位" prop="erpItemUnit">
      <el-input type="text" v-model="form.erpItemUnit" placeholder="请输入单位"></el-input>
      </el-form-item>
      <el-form-item label="说明" prop="erpItemRemark">
      <el-input type="text" v-model="form.erpItemRemark" placeholder="请输入说明"></el-input>
      </el-form-item>
      <el-form-item label="单位重量" prop="erpUnitWeight">
      <el-input type="text" v-model="form.erpUnitWeight" placeholder="请输入单位重量"></el-input>
      </el-form-item>
      <el-form-item label="长" prop="erpUnitLength">
      <el-input type="text" v-model="form.erpUnitLength" placeholder="请输入长"></el-input>
      </el-form-item>
      <el-form-item label="宽" prop="erpUnitWidth">
      <el-input type="text" v-model="form.erpUnitWidth" placeholder="请输入宽"></el-input>
      </el-form-item>
      <el-form-item label="高" prop="erpUnitHeight">
      <el-input type="text" v-model="form.erpUnitHeight" placeholder="请输入高"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
      <el-input type="text" v-model="form.remark" placeholder="请输入备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import {
  getErpItem,
  addOrUpdateErpItem} from "@/api/prodInfor/material";
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    row: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      form: {
        erpItemCode: null,
        erpItemDescCn: null,
        erpItemDescEn: null,
        erpItemUnit: null,
        erpItemRemark: null,
        erpUnitWeight: null,
        erpWeightUomCode: null,
        erpExpirationActionInterval: null,
        erpUnitLength: null,
        erpUnitWidth: null,
        erpUnitHeight: null,
        erpEnabledFlag: null,
        erpEndDateActive: null,
        erpMakeBuyCode: null,
        erpLotControl: null,
        remark: null
      },
      rules: {
        erpItemCode: [
          { required: true, message: "请输入物料编码", trigger: "blur" },
        ],
        erpItemDescCn: [
          { required: true, message: "请输入物料名称（中文）", trigger: "blur" },
        ],
        erpItemDescEn: [
          { required: true, message: "请输入物料名称（英文）", trigger: "blur" },
        ],
        erpItemUnit: [
          { required: true, message: "请输入单位", trigger: "blur" },
        ],
        erpItemRemark: [
          { required: true, message: "请输入说明", trigger: "blur" },
        ],
        erpUnitWeight: [
          { required: true, message: "请输入单位重量", trigger: "blur" },
        ],
        erpWeightUomCode: [
          { required: true, message: "请输入重量单位", trigger: "blur" },
        ],
        erpExpirationActionInterval: [
          { required: true, message: "请输入过期操作间隔", trigger: "blur" },
        ],
        erpUnitLength: [
          { required: true, message: "请输入长", trigger: "blur" },
          { pattern: /^-?\d+(\.\d+)?$/, message: "请输入数字", trigger: "blur" },
        ],
        erpUnitWidth: [
          { required: true, message: "请输入宽", trigger: "blur" },
          { pattern: /^-?\d+(\.\d+)?$/, message: "请输入数字", trigger: "blur" },
        ],
        erpUnitHeight: [
          { required: true, message: "请输入高", trigger: "blur" },
          { pattern: /^-?\d+(\.\d+)?$/, message: "请输入数字", trigger: "blur" },
        ],
        erpEnabledFlag: [
          { required: true, message: "请输入有效标记", trigger: "blur" },
        ],
        erpEndDateActive: [
          { required: true, message: "请输入失效日期", trigger: "blur" },
        ],
        erpMakeBuyCode: [
          { required: true, message: "请输入生产或购买代码", trigger: "blur" },
        ],
        erpLotControl: [
          { required: true, message: "请输入是否启用批次控制", trigger: "blur" },
        ]
      },
      saveloading: false
    };
  },
  computed: {
    dialogVisible: {
      set(val) {
        this.$emit("update:visible", val)
      },
      get() {
        return this.visible
      }
    }
  },
  methods: {
    handleClose() {
      this.saveloading = false
      this.resetFormData()
    },
    handleOpen() {
      if (!this.row?.id) {
        this.resetFormData();
        return
      }
      getErpItem(this.row.id)
      .then(res=>{
        if (res.code == 200 && res.data) {
          this.form = res.data
        } else {
          this.resetFormData()
        }
      }).catch(() => {
        this.resetFormData()
      })
    },
    handleSubmit() {
      this.saveloading = true
      this.$refs.ruleForm
      .validate((flag) => {
        if (flag) {
          addOrUpdateErpItem(this.form).then(res => {
            if (res.code == 200) {
              this.msgSuccess("操作成功")
              this.$emit("submit-success")
              this.dialogVisible = false
            } else {
              this.msgError(res.msg || "操作失败")
            }
            this.saveloading = false
          }).catch(() => {
            this.msgError("操作失败")
            this.saveloading = false
          })
        } else {
          this.saveloading = false
        }
      })
    },
    resetFormData() {
      this.$refs.ruleForm?.resetFields()
      this.form = {
        erpItemCode: null,
        erpItemDescCn: null,
        erpItemDescEn: null,
        erpItemUnit: null,
        erpItemRemark: null,
        erpUnitWeight: null,
        erpWeightUomCode: null,
        erpExpirationActionInterval: null,
        erpUnitLength: null,
        erpUnitWidth: null,
        erpUnitHeight: null,
        erpEnabledFlag: null,
        erpEndDateActive: null,
        erpMakeBuyCode: null,
        erpLotControl: null,
        remark: null
      };
    },
  },
};
</script>
<style lang="scss" scoped>
  ::v-deep.material-dialog-wrap{
    .el-dialog .el-dialog__body{
      max-height: calc(100vh - 14rem);
      .el-form {
        display: flex;
        justify-content: space-between;
        flex-flow: wrap;
        .el-form-item {
          width: 49%;
        }
        .el-date-editor.el-input,
        .el-select {
          width: 100%;
        }
      }
    }
  }
</style>
